export default class AchievementSystem {
    constructor(scene) {
        this.scene = scene;
        this.achievements = [
            { id: 'first_cat', name: '第一只猫', unlocked: false, desc: '获得第一只猫咪' },
            { id: 'coffee_master', name: '咖啡大师', unlocked: false, desc: '完成10杯咖啡订单' }
        ];
        
        this.setupEventListeners();
    }

    setupEventListeners() {
        // 监听游戏事件
        this.scene.events.on('unlock_cat', () => this.checkCondition('first_cat'));
        this.scene.events.on('complete_order', (type) => {
            if (type === 'coffee') this.checkCondition('coffee_master');
        });
    }

    checkCondition(achievementId) {
        const achievement = this.achievements.find(a => a.id === achievementId);
        if (!achievement || achievement.unlocked) return;

        // 条件检查逻辑
        switch(achievementId) {
            case 'first_cat':
                if (this.scene.registry.get('unlockedCats')?.length > 0) {
                    this.unlock(achievementId);
                }
                break;
            case 'coffee_master':
                const count = this.scene.registry.get('coffeeOrders') || 0;
                if (count >= 10) {
                    this.unlock(achievementId);
                }
                break;
        }
    }

    unlock(id) {
        const achievement = this.achievements.find(a => a.id === id);
        if (achievement && !achievement.unlocked) {
            achievement.unlocked = true;
            this.showPopup(achievement);
            return true;
        }
        return false;
    }

    showPopup(achievement) {
        // 成就解锁弹窗
        const popup = this.scene.add.container(540, 300);
        popup.add(this.scene.add.graphics()
            .fillStyle(0x8B4513, 0.9)
            .fillRoundedRect(-200, -100, 400, 200, 16));
        
        popup.add(this.scene.add.text(0, -50, '成就解锁!', { 
            fontFamily: 'pixel_font', 
            fontSize: '32px',
            color: '#FFD700'
        }).setOrigin(0.5));
        
        popup.add(this.scene.add.text(0, 0, achievement.name, { 
            fontFamily: 'pixel_font',
            fontSize: '24px'
        }).setOrigin(0.5));
        
        // 3秒后自动消失
        this.scene.time.delayedCall(3000, () => popup.destroy());
    }
}